Introduction to R and RStudio
- Working with packages
- Getting help in R
Reproducible data analysis with R Markdown
Organise your work with R Projects
Introduction to R and RStudio
Reproducible data analysis with R Markdown
Organise your work with R Projects
Every version of R that is released is named after a topic in a Peanuts comic. The R version 4.3.3 (2024-02-29) is called “Angel Food Cake”.
| R version | Charlie Brown cartoon |
|---|---|
When you start RStudio and R only the base packages are activated: the basic installation with basic functionality.
There are almost 20.000 packages that have been developed by R users all over the world. See the Comprehensive R Archive Network (CRAN)
Not efficient to have all these packages installed every time you use R. Install only the packages you want to use.
Use sessionInfo() to see which packages are active.
This is how the basic installation looks like:
An overview of the packages you have installed, see the tab “Packages” in the output pane:
Packages are like apps on your mobile phone.
When you want to use a package for the first time, you have to install the package.
Each time you want to use the package, you have to load (activate) it.
To load a package use the following code (similar to opening an app on your phone):
library(ggplot2)
To close a package use (similar to closing an app on your phone):
detach(ggplot2)
The need to combine code and text and to document all the steps to make reproducible (scientific) reports of data analyses.
To collaborate effectively and use version control with Github.
It is efficient. Generate and update reports in all kinds of formats:
See the R Markdown Cheat Sheet for a complete list of options.
Code chunks start with {r } (for R code). You can give code chunks names (here cars).
This is how the result looks like in the rendered html document. Display of both R code and results:
You can choose to hide the R code with echo=FALSE in the chunk header:
See the R Markdown Cheat Sheet for a complete list of knitr chunk options.
R Markdown Cheat Sheet:
Every time you start a new (data analysis) project, make it a habit to create a new RStudio Project.
Because you want your project to work:
RStudio Projects create a convention that guarantees that the project can be moved around on your computer or onto other computers and will still “just work”. It creates relative paths (no more broken paths!).
All data, scripts, and output should be stored within the project directory.
Every time you want to work on this project: open the project by clicking the .Rproj file.